#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<map>

using namespace std;

string evaluate(string s, vector<vector<string>>& knowledge) {
    string res;
    map<string, string> m;
    for (int i = 0; i < knowledge.size(); i++) m[knowledge[i][0]] = knowledge[i][1];
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] != '(') res.push_back(s[i]);
        else
        {
            int j = i;
            while (s[j] != ')')  j++;
            string t = s.substr(i + 1, j - i - 1);
            if (m.count(t))  res.append(m[t]);
            else res.push_back('?');
            i = j;
        }
    }
    return res;
}